package gj.cordova.plugin;

import android.app.Activity;
import android.util.Log;

import org.apache.cordova.CallbackContext;
import org.json.JSONArray;
import org.json.JSONObject;

import org.apache.cordova.CordovaPlugin;

import com.tencent.mm.opensdk.modelbase.BaseResp;
import com.tencent.mm.opensdk.modelmsg.SendAuth;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;

public class WX extends CordovaPlugin {
    private Activity activity;
    public static String WEIXIN_KEY = "";
    private IWXAPI api;
    private static CallbackContext callbackContext;
    @Override
    protected void pluginInitialize() {
        Logger.d("init");
        this.activity = cordova.getActivity();
        WEIXIN_KEY = preferences.getString("WEIXIN_KEY", "");
        api = WXAPIFactory.createWXAPI(this.activity, WEIXIN_KEY);
		api.registerApp(WEIXIN_KEY);
    }

    @Override
    public void onPause(boolean multitasking) {

    }
    
    @Override
    public void onResume(boolean multitasking) {

    }

    @Override
    public void onDestroy() {

    }

    @Override
    public boolean execute(String action, JSONArray args, CallbackContext callbackContext) {
        Logger.d("execute");
        JSONObject obj = args.optJSONObject(0);
        if (action.equals("login")) {
            login(callbackContext);
        } else if(action.equals("isInstalled")){
            isInstalled(callbackContext);
        }else{
            return false;
        }
        return true;
    }

    private void login (CallbackContext callbackContext) {
        Logger.d("login");
        SendAuth.Req req = new SendAuth.Req();
        req.scope = "snsapi_userinfo";
        req.state = "web_app_state";
        api.sendReq(req);
        WX.callbackContext = callbackContext;
    }

    private void isInstalled (CallbackContext callbackContext) {
        Logger.d("isInstalled");
        boolean result = api.isWXAppInstalled();
        callbackContext.success(result?1:0);
    }

    public static void loginResult (SendAuth.Resp resp) {
        Logger.d("loginResult");
        if (resp.errCode == BaseResp.ErrCode.ERR_OK) {
            JSONObject jsonObject = new JSONObject();
            try {
                jsonObject.put("state", resp.state);
                jsonObject.put("code", resp.code);
                jsonObject.put("lang", resp.lang);
                jsonObject.put("country", resp.country);
                WX.callbackContext.success(jsonObject);
            } catch (Exception e) {
                WX.callbackContext.error("json error");
            }
        } else {
            WX.callbackContext.error("resp error");
        }
    }

    private static class Logger {
        private static String TAG = "WX";
        private static boolean DEBUG = true;
        public static void d(String str) {
            if (DEBUG) {
                Log.d(TAG, str);
            }
        }

        public static void d(String str1, String str2) {
            if (DEBUG) {
                Log.d(TAG, str1+":"+str2);
            }
        }
    }
}
